<template>
  <el-card>
     <!-- 面包屑导航 -->
    <el-breadcrumb  separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>个人中心</el-breadcrumb-item>
      <el-breadcrumb-item>更改密码</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 表单 -->
    <el-form ref="formRef" label-width="100px" :model="form" :rules="formRules" style="width: 500px;margin-top: 30px">
      <el-form-item label="原密码" prop="old_pwd">
        <el-input v-model="form.old_pwd"></el-input>
      </el-form-item>
       <el-form-item label="新密码" prop="new_pwd">
        <el-input  v-model="form.new_pwd"></el-input>
      </el-form-item>
        <el-form-item label="确认新密码" prop="re_pwd">
        <el-input v-model="form.re_pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updatePwd">修改密码</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { reqUpdatePwd } from '@/api/user'
export default {
  name: 'ResetPwd',
  data () {
    // 校验新旧密码不能一样
    const checkNewPwd = (rule, value, cb) => {
      if (value === this.form.old_pwd) {
        return cb(new Error('新旧密码不能一致'))
      }
      cb()
    }
    // 校验新密码和确认密码一致
    const checkPwdRepeat = (rule, value, cb) => {
      // if (value === this.form.old_pwd) {
      //   return cb(new Error('确认密码不能与旧密码一致'))
      // }
      if (value !== this.form.new_pwd) {
        return cb(new Error('确认密码与新密码必须一致'))
      }
      cb()
    }
    return {
      form: {
        old_pwd: '', // 旧密码
        new_pwd: '', // 新密码
        re_pwd: '' // 确认密码
      },
      // 表单校验规则
      formRules: {
        old_pwd: [
          { required: true, message: '旧密码不能为空', trigger: ['change', 'blur'] },
          { pattern: /^\S{6,15}$/, message: '长度要求在6-15字符之间', trigger: ['change', 'blur'] }
        ],
        new_pwd: [
          { required: true, message: '新密码不能为空', trigger: ['change', 'blur'] },
          { pattern: /^\S{6,15}$/, message: '长度要求在6-15字符之间', trigger: ['change', 'blur'] },
          { validator: checkNewPwd, trigger: ['change', 'blur'] }
        ],
        re_pwd: [
          { required: true, message: '确认密码不能为空', trigger: ['change', 'blur'] },
          { pattern: /^\S{6,15}$/, message: '长度要求在6-15字符之间', trigger: ['change', 'blur'] },
          { validator: checkPwdRepeat, trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  methods: {
    // 重置
    reset () {
      this.$refs.formRef.resetFields()
    },
    // 更改密码
    updatePwd () {
      // 1. 预校验
      this.$refs.formRef.validate(async boo => {
        if (!boo) return ''
        // 2.发送请求
        const { data } = await reqUpdatePwd(this.form)
        // 3. 提示
        if (data.code !== 0) return this.$message.error(data.message)
        this.$message.success(data.message)
        // 4. 清除token并重新登录
        localStorage.removeItem('bigevent-token')
        this.$router.push('/login')
      })
    }
  }
}
</script>

<style>

</style>
